const Service = require("uni-cloud-router").Service;
module.exports = class UserService extends Service{
	async getHistoryProject({wkID,pageNo,pageSize}){
		let res = await this.db.collection('signin_store')
		.aggregate()
		.match({
			wkID
		})
		.skip((pageNo-1)*pageSize)
		.limit(pageSize)
		.end()
		
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			return {
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async filtrateSign({wkID,curYearMonth}){
		let reg = new RegExp(curYearMonth.trim(),'ig')
		let res = await this.db.collection("signin_store").where({
			wkID,
			date:reg
		}).get()
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			return {
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getUnderwayProject({wkID,projectStatus}){
		const dbCmd = this.db.command;
		let res = await this.db.collection('project_store').where({
			project_status:projectStatus,
			worker_ids:dbCmd.elemMatch({
				wkID:wkID
			})
		}).get()
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			return{
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async signin({data}){
		let res = await this.db.collection('signin_store').add(data);
		
		if(res.affectedDocs !== 0){
			return {
				status:0,
				msg:"打卡成功"
			}
		} else {
			return {
				status:1,
				msg:"打卡失败"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
}